/*******************************************
  Jose Andres Lara Vecino

  Author       : $Author: cainlara@gmail.com $
  Last CheckIn : $Date: 2012-08-22 18:35:06 +0000 (Wed, 22 Aug 2012) $
  Revision     : $Revision: 100 $
*******************************************/

package generico;

import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JDialog;
import javax.swing.WindowConstants;

import control.App;

/**
 * Super clase para los dialogos.
 * 
 * @author jose.lara 07.06.2012
 */
public abstract class GenericoDialogo extends JDialog {
    private static final long serialVersionUID = 1L;
    private static final int ESC                = 27;
    
    public GenericoDialogo() {
        this(null, true);
    }
    
    public GenericoDialogo(String titulo) {
        this(titulo, true);
    }
    
    public GenericoDialogo(String titulo, boolean modal) {
        super(App.getVentana(), titulo, modal);
        agregarKeyListener();
        centrar();
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        pack();
    }
    
    @Override
    public boolean isFocusable() {
        return true;
    }
    
    protected abstract void guardar();
    
    protected void guardarCerrar() {
        guardar();
        cerrarDialogo();
    }
    
    private void centrar() {
        int x = 0;
        int y = 0;
        
        x = (int)((App.getVentana().getWidth() - getSize().getWidth()) / 2);
        y = (int)((App.getVentana().getHeight() - getSize().getHeight()) / 2);
        
        setLocation(new Point(x, y));
    }
    
    private void agregarKeyListener() {
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == ESC) {
                    dispose();
                }
            }
        });
    }
    
    protected void cerrarDialogo() {
        dispose();
    }
}
